package com.bdsoft.actions.departmentManage.Service.impl;

import cn.hutool.core.map.MapUtil;
import com.bdsoft.actions.UserManage.Dao.UserMapper;
import com.bdsoft.actions.departmentManage.DTO.DepartmentDTO;
import com.bdsoft.actions.departmentManage.Dao.DepartmentMapper;
import com.bdsoft.actions.departmentManage.Service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public int addDepartment(DepartmentDTO department) {
        return departmentMapper.insertDepartment(department);
    }

    @Override
    public int updateDepartment(DepartmentDTO department) {
        return departmentMapper.updateDepartment(department);
    }

    @Override
    public int deleteDepartment(String deptId) {
        return departmentMapper.deleteDepartment(deptId);
    }

    @Override
    public Map<String, Object> getDepartmentById(String deptId) {

        return MapUtil.<String,Object>builder()
                .put("code",200)
                .put("data",departmentMapper.selectDepartmentById(deptId))
                .build();
    }

    @Override
    public Map<String,Object> getAllDepartments() {

        List<Map<String,Object>> list=departmentMapper.selectAllDepartments();

        return MapUtil.<String,Object>builder()
                .put("code",200)
                .put("data",buildMenuTree(list))
                .build();

    }

    @Override
    public Map<String, Object> getDepartMentUser() {
        List<Map<String,Object>> userList=userMapper.selectUserByParams("","", String.valueOf(1),"");
        List<Map<String,Object>> depList=departmentMapper.selectAllDepartments();

        for (Map<String,Object> dep:depList){
            dep.put("hasUser", new  ArrayList<>());
        }
        for (Map<String,Object> dep:depList){
            List<Map<String, Object>> userOp = Optional.ofNullable(userList)
                    .orElse(Collections.emptyList())
                    .stream()
                    .filter(m -> String.valueOf(m.get("department_id")).equals(String.valueOf(dep.get("deptId"))))
                    .toList();

             List<Map<String, Object>> depUserList = userOp.stream().collect(Collectors.toList());
            dep.put("hasUser",depUserList);

         }
         return MapUtil.<String,Object>builder()
                .put("code",200)
                .put("data",buildMenuTree(depList))
                .build();
    }

    public static List<Map<String, Object>> buildMenuTree(List<Map<String, Object>> flatMenus) {
        // 用Map存储所有菜单（按menu_id索引）
        Map<String, Map<String, Object>> menuMap = new HashMap<>();

        // 初始化所有菜单节点，并添加children占位
        for (Map<String, Object> menu : flatMenus) {
            menu.put("children", new ArrayList<>());
            menuMap.put((String) menu.get("deptId"), menu);
        }

        // 构建树形结构
        List<Map<String, Object>> tree = new ArrayList<>();
        for (Map<String, Object> menu : flatMenus) {
            String parentId = (String) menu.get("parentId");
            if ("-1".equals(parentId)) {
                // 顶级菜单
                tree.add(menu);
            } else {
                // 子菜单：添加到父菜单的children中
                Map<String, Object> parent = menuMap.get(parentId);
                if (parent != null) {
                    ((List<Map<String, Object>>) parent.get("children")).add(menu);
                }
            }
        }

        return tree;
    }
} 